﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Runda.CMS.Order.Repository;
using Runda.CMS.Order.Entity;

namespace OrderPortal.Models
{
    #region Models    

    public class LogOnModel
    {
        [Required]
        [DisplayName("用户名")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [DisplayName("密码")]
        public string Password { get; set; }
    }
     
    #endregion

    #region Services

    public interface IMembershipService
    {   
        bool ValidateUser(string userName, string password,out Account account);
        bool ChangePassword(string userName, string oldPassword, string newPassword, out string msg);
    }

    public class AccountMembershipService : IMembershipService
    {
        private AccountRepository accountRepository;

        public AccountMembershipService() 
        {
            accountRepository = new AccountRepository();
        }
 
        public bool ValidateUser(string userName, string password,out Account account)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("用户名不能为空", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("密码不能为空", "password");
            account = null;
            List<Account> accounts = accountRepository.findAccountByNameAndPassword(userName, password);
            if (accounts != null && accounts.Count > 0) {
                account = accounts[0];
                return true;
            }
            return false;
        }
       
        public bool ChangePassword(string accountName,string oldPassword, string newPassword,out string msg)
        {                                        
            List<Account> accounts = accountRepository.findAccountByNameAndPassword(accountName, oldPassword);               
            if (accounts != null && accounts.Count > 0) {
                Account ac = accounts[0];
                ac.resetPassword(newPassword);
                accountRepository.merge(ac);
                msg = "";
                return true;
            }
            msg = "orginal password inconsistent";
            return false;                    
        }
    }

    public interface IFormsAuthenticationService
    {
        void SignIn(Account account);
        void SignOut();
    }

    public class FormsAuthenticationService : IFormsAuthenticationService
    {
        public void SignIn(Account account)
        {
            HttpContext httpcontext = HttpContext.Current;
            httpcontext.Session["currentUser"] = account;
        }

        public void SignOut()
        {
            HttpContext httpcontext = HttpContext.Current;
            httpcontext.Session.Abandon();
        }
    }
    #endregion   
}
